package cn.lg.soar.system.biz.modules.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.SysConfigTypeEnum;
import cn.lg.soar.system.biz.modules.general.entity.SysConfig;
import cn.lg.soar.system.biz.modules.general.service.ISysConfigService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 系统配置
 * @author luguoxiang
 * @since 2023-07-13
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Tag(name = "系统配置")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sys-config")
public class SysConfigController extends BaseController<SysConfig, ISysConfigService> {

    @Override
    protected Serializable getId(SysConfig entity) {
        return entity.getCode();
    }


    @GetMapping(value = "{code}", name = "根据code获取配置")
    public HttpResult<String> get(@PathVariable("code") String code) {
        return HttpResult.data(service.get(code));
    }

    @PermitDefines({
            @PermitDefine(code = "pc", name = "修改pc端配置"),
            @PermitDefine(code = "app", name = "修改移动端端配置"),
    })
    @DisabledTenantAccess
    @Log(value = "设置", type = LogTypeEnum.OTHER)
    @IdempotentByParam(value = "#p1", expire = 30_000)
    @PostMapping(value = "set/{code}", name = "根据code修改配置")
    public void set(@PathVariable("code") String code, @RequestBody String value) {
        service.set(code, value);
    }

    @DisabledTenantAccess
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.createResult(data);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.updateResult(data);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        service.remove(
                Wrappers.<SysConfig>lambdaQuery()
                        .in(SysConfig::getId, ids)
                        .eq(SysConfig::getType, SysConfigTypeEnum.other)
        );
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<SysConfig> detail(@RequestParam(name = "id") Long id) {
        SysConfig config = service.getOne(
                Wrappers.<SysConfig>lambdaQuery()
                        .eq(SysConfig::getId, id)
                        .eq(SysConfig::getType, SysConfigTypeEnum.other)
        );
        return HttpResult.data(config);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<SysConfig>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(SysConfig::getType, ConditionEnum.eq, SysConfigTypeEnum.other.getValue());
        return super.queryResult(pageQuery, params);
    }

    @DisabledTenantAccess
    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

    @DisabledTenantAccess
    @Operation(summary = "清空缓存")
    @PostMapping(value = "clear-cache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }

}

